using System;
using System.Collections;
using System.DirectoryServices;

namespace BEGDC.CC.Entity
{
	#region SystemFunction

	/// <summary>
	/// SystemFunction object for NHibernate mapped table 'SystemFunction'.
	/// </summary>
	[Serializable]
	public class SystemFunction : System.IComparable
		{
		#region Member Variables
		
		protected int _id;
		protected string _functionName;
		protected string _functionURL;
		protected string _description;
		protected int _parentFunction;
		protected int _sort;
		protected bool _isVisibled;
		protected static String _sortExpression = "Id";
		protected static SortDirection _sortDirection = SortDirection.Ascending;

		#endregion

		#region Constructors

		public SystemFunction() { }

		public SystemFunction( string functionName, string functionURL, string description, int parentFunction, int sort, bool isVisibled )
		{
			this._functionName = functionName;
			this._functionURL = functionURL;
			this._description = description;
			this._parentFunction = parentFunction;
			this._sort = sort;
			this._isVisibled = isVisibled;
		}

		#endregion

		#region Public Properties

		public int Id
		{
			get {return _id;}
			set {_id = value;}
		}

		public string FunctionName
		{
			get { return _functionName; }
			set
			{
				if ( value != null && value.Length > 50)
					throw new ArgumentOutOfRangeException("Invalid value for FunctionName", value, value.ToString());
				_functionName = value;
			}
		}

		public string FunctionURL
		{
			get { return _functionURL; }
			set
			{
				if ( value != null && value.Length > 100)
					throw new ArgumentOutOfRangeException("Invalid value for FunctionURL", value, value.ToString());
				_functionURL = value;
			}
		}

		public string Description
		{
			get { return _description; }
			set
			{
				if ( value != null && value.Length > 200)
					value = value.Substring(0, 200 );
				_description = value;
			}
		}

		public int ParentFunction
		{
			get { return _parentFunction; }
			set { _parentFunction = value; }
		}

		public int Sort
		{
			get { return _sort; }
			set { _sort = value; }
		}

		public bool IsVisibled
		{
			get { return _isVisibled; }
			set { _isVisibled = value; }
		}

        public static String SortExpression
        {
            get { return _sortExpression; }
            set { _sortExpression = value; }
        }

        public static SortDirection SortDirection
        {
            get { return _sortDirection; }
            set { _sortDirection = value; }
        }
		#endregion
		
        #region IComparable Methods
        public int CompareTo(object obj)
        {
			if (!(obj is SystemFunction))
				throw new InvalidCastException("This object is not of type SystemFunction");
			
			int relativeValue;
			switch (SortExpression)
			{
				case "Id":
					relativeValue = this.Id.CompareTo(((SystemFunction)obj).Id);
					break;
				case "FunctionName":
					relativeValue = this.FunctionName.CompareTo(((SystemFunction)obj).FunctionName);
					break;
				case "FunctionURL":
						relativeValue = ( this.FunctionURL != null ) ? this.FunctionURL.CompareTo(((SystemFunction)obj).FunctionURL) : -1;
					break;
				case "Description":
						relativeValue = ( this.Description != null ) ? this.Description.CompareTo(((SystemFunction)obj).Description) : -1;
					break;
				case "ParentFunction":
						relativeValue = ( this.ParentFunction != Convert.ToInt32( null) ) ? this.ParentFunction.CompareTo(((SystemFunction)obj).ParentFunction) : -1;
					break;
				case "Sort":
						relativeValue = ( this.Sort != Convert.ToInt32( null) ) ? this.Sort.CompareTo(((SystemFunction)obj).Sort) : -1;
					break;
				case "IsVisibled":
						relativeValue = ( this.IsVisibled != Convert.ToBoolean( null) ) ? this.IsVisibled.CompareTo(((SystemFunction)obj).IsVisibled) : -1;				
					break;
                default:
                    goto case "Id";
			}
            if (SystemFunction.SortDirection == SortDirection.Ascending)
				relativeValue *= -1;
			return relativeValue;
		}
		#endregion
	}

	#endregion
}
